package com.mm.gbicc.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.mm.gbicc.bean.Menu;
import com.mm.gbicc.dao.FileDao;

/**
 * Servlet implementation class FileUpload
 */
public class FileUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileUpload() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String parent_id = request.getParameter("idstrd");
		String user_id = request.getParameter("user_id");
		if(user_id==null||"".equals(user_id))
			user_id = "1";
		System.out.println(parent_id);
		FileItemFactory factory = new DiskFileItemFactory(); 
		ServletFileUpload upload = new ServletFileUpload(factory); 
		try { 
		    List items = upload.parseRequest(request); 
		    Iterator iter = items.iterator(); 
		    while (iter.hasNext()) { 
		        FileItem item = (FileItem) iter.next(); 
		        if (item.isFormField()) { 
		            //System.out.println("isFormField----" + item.getName()); 
		        } else { 
		            String fullPathName = item.getName(); 
		            //System.out.println(fullPathName);
		            String rantom =  new Date().getTime()+fullPathName.substring(fullPathName.lastIndexOf("."));
		            String filename = this.getServletContext().getRealPath("upload") + File.separator + rantom;
		            //filename = this.getServletContext().getRealPath("upload") + File.separator + fullPathName;
		           // System.out.println(filename);
		            File file = new File(filename); 
		            try { 
		                item.write(file); 
		                
		                //要保存到数据库的文件名，完整地址为：http://ip:port/fileManage/upload/rantom
		                String url = "/report/http://"+request.getServerName()+":"+request.getServerPort()+"/fileManage/upload/"+rantom;
		                String menu_name = fullPathName.substring(0,fullPathName.lastIndexOf("."));
		                System.out.println(menu_name);
		                
		                FileDao fileDao = new FileDao();
		                Menu menu = new Menu();
		                menu.setName_cn(menu_name);
		                menu.setNote(menu_name);
		                menu.setUrl(url);
		                menu.setParent_id(parent_id);
		                menu.setCreate_user_id(user_id);
		                if(fileDao.addMenu(menu)){
		                	 response.getWriter().write("文件"+fullPathName+"上传成功");
		                }
		            } catch (Exception e) { 
		                e.printStackTrace(); 
		            }
		        } 
		    } 
		} catch (Exception e) { 
		    e.printStackTrace(); 
		} 
	}
	public static void main(String[] args) {
		String fullPathName= "华润万家4I-价格监控月报(2013.09).pdf";
		String menu_name = fullPathName.substring(0,fullPathName.lastIndexOf("."));
        System.out.println(menu_name);
	}
}
